//
//  XinzuoZone.m
//  LiuHeBao
//
//  Created by zr on 2018/8/23.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "XinzuoZone.h"

@interface XinzuoZone ()

@property (nonatomic, strong)UIButton * shengButton;
@property (nonatomic, strong)UIButton * briButton;
@property (nonatomic, strong)UILabel * shengLabel;
@property (nonatomic, strong)UILabel * briLabel;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UIButton * startButton;
//@property (nonatomic, weak)UITextView * textView;
@property(nonatomic,weak)UIWebView *webView;

@end

@implementation XinzuoZone


- (void)setShengStr:(NSString *)shengStr{
    
    _shengStr = shengStr;
    
    [self.shengButton setTitle:shengStr forState:UIControlStateNormal];
}

- (void)setBriStr:(NSString *)briStr{
    
    _briStr = briStr;
    
    [self.briButton setTitle:briStr forState:UIControlStateNormal];
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = WHITE_COLOR;
    self.userInteractionEnabled = YES;
    
    self.shengLabel = [self setupLabelWith:@"您的星座:"];
    [self addSubview:self.shengLabel];
    self.briLabel = [self setupLabelWith:@"出生年月:"];
    [self addSubview:self.briLabel];
    
    self.shengButton = [self setupChooseButton];
    [self addSubview:self.shengButton];
    self.briButton = [self setupChooseButton];
    [self addSubview:self.briButton];
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"星座分析";
    titleLabel.textColor = MAINCOLOR;
    titleLabel.font = [UIFont systemFontOfSize:20 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self addSubview:self.titleLabel];
    
    
    UIButton * startButton = [UIButton new];
    
    [startButton setTitle:@"开始分析" forState:UIControlStateNormal];
    [startButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [startButton setBackgroundColor:MAINCOLOR];
    [startButton addTarget:self action:@selector(clickStartButton) forControlEvents:UIControlEventTouchUpInside];
    startButton.layer.cornerRadius = 16;
    startButton.layer.masksToBounds = YES;
    startButton.titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    
    self.startButton = startButton;
    
    [self addSubview:self.startButton];
    
    
//    UITextView *textView =[[UITextView alloc] init];
//    textView.backgroundColor= [UIColor whiteColor];
//    textView.text = @"";
//    textView.textColor = [UIColor blackColor];
//    textView.editable = NO;
//    textView.font = [UIFont systemFontOfSize:13 * SCREENH6];
//
//    self.textView = textView;
//
//    [self addSubview:self.textView];
    
    UIWebView *webView = [[UIWebView alloc] init];
    webView.scalesPageToFit = YES;
    //    webView.delegate = self;
    webView.backgroundColor = WHITE_COLOR;
//    NSURL *urlPath = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
//
//    [webView loadHTMLString:self.contentStr baseURL:urlPath];
    
    self.webView = webView;
    
    [self addSubview:self.webView];
}

- (void)layoutSubviews{
    
    [self.shengLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(20 * SCREENH6);
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
    }];
    
    [self.shengButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.shengLabel);
        make.leading.mas_equalTo(self.shengLabel.mas_trailing).offset(10 * SCREENH6);
    }];
    
    [self.briLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shengLabel);
        make.top.mas_equalTo(self.shengLabel.mas_bottom).offset(45 * SCREENH6);
    }];
    
    [self.briButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shengButton);
        make.centerY.mas_equalTo(self.briLabel);
    }];
    
    [self.startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.briButton.mas_bottom).offset(30 * SCREENH6);
        make.height.mas_equalTo(33 * SCREENH6);
        make.width.mas_equalTo(175 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.startButton.mas_bottom).offset(20 * SCREENH6);
    }];
    
//    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self.briLabel);
//        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
//        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5 * SCREENH6);
//        make.bottom.mas_equalTo(self);
//    }];
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.briLabel);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5 * SCREENH6);
        make.bottom.mas_equalTo(self);

    }];
}


- (UILabel *)setupLabelWith:(NSString *)str{
    
    UILabel * label = [UILabel new];
    
    label.textColor = BLACK_COLOR;
    label.font = [UIFont systemFontOfSize:16 * SCREENH6];
    label.text = str;
    
    return label;
}


- (UIButton *)setupChooseButton{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:@"请选择" forState:UIControlStateNormal];
    [button setTitleColor:ColorWithRGB(0xB3B3B3) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickChooseButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    return button;
}


- (void)clickStartButton{
    
    NSString * url = [NSString stringWithFormat:@"/index/Liuhetreasure/getArticle?type=2&title=%@",self.shengButton.titleLabel.text];
    NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
    NSString *encodeUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
    
    [[NetworkTool sharedTools] GET:encodeUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray * data = responseObject[@"data"];
        
        NSDictionary * dict = data.firstObject;
        
//        self.textView.text = dict[@"content"];
        NSURL *urlPath = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
        
        [self.webView loadHTMLString:dict[@"content"] baseURL:urlPath];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}


- (void)clickChooseButton:(UIButton *)button{
    
    
    NSString * type;
    if (button == self.shengButton) {
        
        type = @"xinzuo";
    }else{
        
        type = @"time";
    }
    
    NSDictionary * dict = @{@"type":type};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickXinzuoChoose" object:nil userInfo:dict];
    
}

@end
